﻿using SharpDX;
using SharpDX.Direct3D11;
using System;

namespace Engine.Graphics.Shaders.Wr2Shaders
{
    public class Material112 : ShaderBase, ISetShaderValues
    {
        #region Private Fields

        private Device device;
        private ShaderResourceView envTexture;
        private string name;
        private SamplerState samplerState;
        private SamplerStateDescription samplerStateDescription;

        #endregion Private Fields

        // register(s3)
        // register(t3) /

        #region Public Constructors

        public Material112(Device renderDevice, string shaderDirectory)
            : base(renderDevice)
        {
            this.device = renderDevice;
            this.name = shaderDirectory;
            base.LoadShader(renderDevice, shaderDirectory + "\\material112.fx", "ps_main", "vs_main");
            base.inputLayout = new InputLayout(renderDevice, base.shaderByteCodeVS, Graphics.VertexStructures.MyOwnVertex.MyPositionNormalTextured.elements);

            // Create samplerstate
            samplerStateDescription = new SamplerStateDescription()
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                BorderColor = new Color4(0),
                ComparisonFunction = Comparison.Always,
                Filter = Filter.Anisotropic,
                MaximumAnisotropy = 16,
                MaximumLod = float.MaxValue,
                MinimumLod = 0,
                MipLodBias = 0
            };

            samplerState = new SamplerState(renderDevice, samplerStateDescription);
            using (Resource texture = (Texture2D)Texture2D.FromFile(this.device, shaderDirectory + "\\EnvMap.dds"))
            {
                this.envTexture = new ShaderResourceView(this.device, texture);
            }
            this.device.ImmediateContext.PixelShader.SetShaderResource(3, this.envTexture);
            this.device.ImmediateContext.PixelShader.SetSampler(3, samplerState);
        }

        #endregion Public Constructors

        #region Public Properties

        public ShaderBase Effect
        {
            get
            {
                return this.effect;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion Public Properties

        #region Public Methods

        public new void Dispose()
        {
            this.ps.Dispose();
            this.inputLayout.Dispose();
            this.vs.Dispose();
            this.samplerState.Dispose();
            base.Dispose();
        }

        public void SetProperties(int materialID, MaterialManager materialManager)
        {
            Material material = materialManager.GetMaterial(materialID);
            base.SetSamplerState1(samplerState);
            base.SetTexture1(material.TextureName1, materialManager);
        }

        #endregion Public Methods
    }
}